function makou_z_fixed=BPFF_fix_makou_z(date,t,makou_Q,makou_z)
%利用BP神经网络和马口站流量数据，补充马口站的水位缺失数据
addpath('./BPFF');
param=BPFFTrainParam;
param.trainBegin={[1967 5 1 1 0 0]};
param.trainEnd={[1967 11 1 1 0 0]};
param.predictBegin={[0]};
param.predictEnd={[0]};
param.trainCount=1;
hiddenLayerSize=10;
param=param.date_to_index(t);  %这一大段主要是为了用这个日期转换为索引的函数
trainBegin=param.trainBegin{1};
trainEnd=param.trainEnd{1};


index=find(isnan(makou_z(:)));
makou_Q_nan=makou_Q(index);
makou_z_nan=makou_z(index);

myStream = RandStream('dsfmt19937','Seed',15);   %由于神经网络以随机初始权重开始，因此结果在每次运行时都会略有不同,通过设置随机种子避免这种随机性。
RandStream.setGlobalStream(myStream);

% tonndata: Convert data to standard neural network cell array form(to neural network data)
% 可以输入多个序列，第二个变量为false时代表每一行为一个时间步，第三个变量我忘了是啥东西
columnSamples = false;   % samples are by rows.
cellTime = false;     % time-steps in matrix, not cell array.
[inputSeries,wasMatrixIn] = tonndata(makou_Q(trainBegin:trainEnd),columnSamples,cellTime);
[TargetSeries,wasMatrixTar] = tonndata(makou_z(trainBegin:trainEnd),columnSamples,cellTime);
trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.
FF_net = feedforwardnet(hiddenLayerSize,trainFcn);

%设定
FF_net.performFcn='mse';    %Mean squared error
FF_net.divideParam.trainRatio = 70/100;
FF_net.divideParam.valRatio = 15/100;
FF_net.divideParam.testRatio = 15/100;
FF_net.divideFcn='dividerand';  %按块划分，还有一种是随机划分dividerand（默认值）
FF_net.divideMode='value';
FF_net.trainParam.epochs=10000;   %最大步数

% 开始训练网络
[FF_net,tr] = train(FF_net,inputSeries,TargetSeries);



%% 进行多步时间预测

[inputSeries_predict,wasMatrixIn] = tonndata(makou_Q_nan,columnSamples,cellTime);
output = FF_net(inputSeries_predict);
output = fromnndata(output,wasMatrixTar,columnSamples,cellTime);

for i=1:length(index)
    makou_z(index(i))=output(i);
end
makou_z_fixed=makou_z;

end
